package com.easy.tool.utils.str;

import com.easy.tool.anno.BeanDes;
import com.easy.tool.anno.MethodDes;
import com.easy.tool.exception.ExceptionUtils;
import com.easy.tool.utils.rule.RuleUtils;
import org.apache.log4j.Logger;

/**
 * @author 高俊
 * @create 2021-09-2021/9/27-14:33
 */
@BeanDes(name = "字符串工具",des = "用于对字符串进行操作的工具")
public class StringUtils {

    private static Logger logger = Logger.getLogger(StringUtils.class);

    /**
     * 转驼峰命名
     * @param str
     * @return
     */
    @MethodDes(des = "转驼峰命名",explain = "需要a_b的格式",param = {"arg0-> str：字符串"})
    public static String getHumpName(String str){
        boolean bool = ExceptionUtils.humpDecide(str);
        if (bool){
            int index = str.indexOf("_");
            if (index > 0){
                String newStr = str.substring(index + 1, index + 2).toUpperCase();
                str = str.replace(str.substring(index, index + 2),newStr);
                str = getHumpName(str);
            }
        }
        return str;
    }

    /**
     * 退回常规下划线命名
     * @param str
     * @return
     */
    @MethodDes(des = "退回常规下划线命名",explain = "需要helloWorld的格式",param = {"arg0-> str：字符串"})
    public static String backHumpName(String str){
        StringBuilder builder = new StringBuilder();
        if (ExceptionUtils.nullDecide(str)){

            int index = 0;
            for (int i = 0; i < str.length(); i++) {
                char curChar = str.charAt(i);
                if (curChar >= 'A' && curChar <= 'Z'){
                    builder.append(str.substring(index,i).toLowerCase()).append("_");
                    index = i;
                }
                if(i == str.length()-1){
                    builder.append(str.substring(index).toLowerCase());
                }
            }
            if (builder.length() == 0){
                logger.warn("无大写字符，不符合驼峰命名格式");
            }
            return builder.toString();
        }
        return builder.toString();
    }


    /**
     * 首字母转大写
     * @param str
     * @return
     */
    @MethodDes(des = "首字母转大写",param = {"arg0-> str：字符串"})
    public static String toUpperCaseFirst(String str){
        boolean bool = ExceptionUtils.nullDecide(str);
        if (bool){
            String firstChar = str.substring(0, 1);
            String otherChar = str.substring(1);
            firstChar = firstChar.toUpperCase();
            str = firstChar + otherChar;
        }

        return str;
    }
    /**
     * 首字母转小写
     * @param str
     * @return
     */
    @MethodDes(des = "首字母转小写",param = {"arg0-> str：字符串"})
    public static String toUpperLowFirst(String str){
        boolean bool = ExceptionUtils.nullDecide(str);
        if (bool){
            String firstChar = str.substring(0, 1);
            String otherChar = str.substring(1);
            firstChar = firstChar.toLowerCase();
            str = firstChar + otherChar;
        }
        return str;
    }

    /**
     * 判断字符串是否为 空串
     * @param str
     * @return
     */
    @MethodDes(des = "判断字符串是否为 空串",param = {"arg0-> str：字符串"})
    public static boolean isBlank(String str){
        return RuleUtils.createRule(str).eq("").over();
    }

    /**
     * 判断字符串是否为NULL
     * @param str
     * @return
     */
    @MethodDes(des = "判断字符串是否为NULL",param = {"arg0-> str：字符串"})
    public static boolean isNull(String str){
        return RuleUtils.createRule(str).eq(null).over();
    }



    /**
     * 判断某个参数是否在数组内
     * @param child 指定参数
     * @param array 指定数组
     * @return
     */
    @MethodDes(des = "判断某个参数是否在数组内",param = {"arg0-> str：指定参数","arg1-> array：指定数组"})
    public static <T> boolean contains(T child,T[] array){
        for (int i = 0; i < array.length; i++) {
            if (child.equals(array[i]) || child == array[i]){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
    /**
     * 模糊判断判断某个参数是否在数组内
     * @param child 指定参数
     * @param array 指定数组
     * @return
     */
    @MethodDes(des = "判断某个参数是否在数组内",param = {"arg0-> str：指定参数","arg1-> array：指定数组"})
    public static <T> boolean containsLike(String child,String[] array){
        for (int i = 0; i < array.length; i++) {
            if (child.indexOf(array[i]) >= 0 || array[i].indexOf(child) >= 0){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
}
